package com.bnz.servlet;

import cn.hutool.core.util.StrUtil;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import com.bnz.service.impl.ClassesServiceImpl;
import com.bnz.service.impl.StudentServiceImpl;
import org.omg.CORBA.INTERNAL;
import org.omg.CosNaming.BindingIterator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;
    private ClassesService classesService;
    @Override
    public void init() throws ServletException {
        studentService = new StudentServiceImpl();
        classesService = new ClassesServiceImpl();
    }

    /**
     * 1. 理解转发与重定向
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.1 得到所有学生列表
        // List<Student> students =  studentService.findAll();
        //1.2 将数据放到请求作用域中
        // req.setAttribute("students",students);
        //1.3 转发数据到页面（转发是一次请求，可以保存住数据，地址栏地址不会发生改变，是服务器内部跳转）
        // req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req,resp);

        //1.4 重定向（两次请求，不能保存数据，地址栏地址会发生改变）
        //resp.sendRedirect("/WEB-INF/jsp/student/list.jsp");


        req.setCharacterEncoding("utf-8");
        // 1.1 根据请求参数调用不同 的方法
        String cmd = req.getParameter("cmd");
        // 1.2 根据参数调用方法
        if(StrUtil.isNotBlank(cmd)){
            if("list".equals(cmd)){         // 查询所有
                list(req,resp);
            }
            if("findByPage".equals(cmd)){   // 分页查询
                findByPage(req,resp);
            }
            if("search".equals(cmd)){       // 条件查询带分页
                search(req,resp);
            }
            if("add".equals(cmd)){        // 添加学生
                add(req,resp);
            }
            if("update".equals(cmd)){     // 修改学生
                update(req,resp);
            }

        }
    }


    //2. 列表所有学生
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //2.1 得到所有的学生列表
        List<Student> students = studentService.findAll();
        //2.2 将学生列表放到请求作用域中
        req.setAttribute("students",students);
        //2.3 进行页面跳转
        req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req,resp);
    }

    //3. 分页查询
    private void findByPage(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        //3.1 得到分页参数，设置默认参数
        int page = 1;           // 当前页
        int pagesize = 5;       // 每页大小
        //3.2 得到前端传入的参数
        String pageStr = req.getParameter("page");
        String pagesizeStr = req.getParameter("pagesize");
        page = StrUtil.isNotBlank(pageStr) ? new Integer(pageStr) : page;
        pagesize = StrUtil.isNotBlank(pagesizeStr) ? new Integer(pagesizeStr) : pagesize;
        //3.3 开始分页查询
        PageResult<Student> pr = studentService.findByPage(page,pagesize);
        //3.4 查询所有班级
        List<Classes> classes = classesService.findAll();
        //3.5 放到请求作用域中
        req.setAttribute("pr",pr);
        req.setAttribute("classes",classes);
        //3.6 进行转发
        req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req,resp);


    }
    //3. 分页查询带条件查询
    private void search(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        //3.1 得到分页参数，设置默认参数
        int page = 1;           // 当前页
        int pagesize = 5;       // 每页大小
        //3.2 得到前端传入的参数
        String pageStr = req.getParameter("page");
        String pagesizeStr = req.getParameter("pagesize");
        page = StrUtil.isNotBlank(pageStr) ? new Integer(pageStr) : page;
        pagesize = StrUtil.isNotBlank(pagesizeStr) ? new Integer(pagesizeStr) : pagesize;
        //3.3 得到查询的条件参数
        String sname = req.getParameter("sname");
        String addr = req.getParameter("addr");
        String cid = req.getParameter("cid");
        if(StrUtil.isBlank(sname)){
            sname = "";
        }
        if(StrUtil.isBlank(addr)){
            addr = "";
        }
        if(StrUtil.isBlank(cid)){
            cid = "0";
        }
        //3.4 封装成对象
        StudentVo vo = new StudentVo(sname,addr,new Integer(cid));
        //3.5 开始分页查询
        PageResult<Student> pr = studentService.search(page,pagesize,vo);
        //3.6 查询所有班级
        List<Classes> classes = classesService.findAll();
        //3.7 放到请求作用域中
        req.setAttribute("pr",pr);
        req.setAttribute("classes",classes);
        //3.8 进行转发
        req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req,resp);
    }

    //4. 添加学生
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //4.1 收集表单信息
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String addr = req.getParameter("addr");
        String cid = req.getParameter("cid");
        //4.2 构造成学生对象
        Student student = new Student(sname,new Integer(age),addr,new Integer(cid),sex);

        //4.3 添加学生
        studentService.insert(student);
        //4.4 进行页面的重定向 (不要使用转发)
        // req.getRequestDispatcher("/student?cmd=search").forward(req,resp);
        resp.sendRedirect("/student?cmd=search");

    }
    // 5. 修改学生
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //5.1 收集表单信息
        String sid = req.getParameter("sid");
        String sname = req.getParameter("sname");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String addr = req.getParameter("addr");
        String cid = req.getParameter("cid");
        //5.2 构造成学生对象
        Student student = new Student(new Integer(sid),sname,new Integer(age),addr,new Integer(cid),sex);

        //5.3 修改学生
        studentService.update(student);
        //5.4 进行页面的重定向 (不要使用转发)
        // req.getRequestDispatcher("/student?cmd=search").forward(req,resp);
        resp.sendRedirect("/student?cmd=search");
    }



}
